package org.nullauthor.allthings.gui.handler;

import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.entity.player.PlayerInventory;
import net.minecraft.inventory.Inventory;
import net.minecraft.inventory.SimpleInventory;
import net.minecraft.item.ArmorItem;
import net.minecraft.item.ItemStack;
import net.minecraft.item.Items;
import net.minecraft.item.ToolItem;
import net.minecraft.screen.*;
import net.minecraft.screen.slot.ForgingSlotsManager;
import net.minecraft.screen.slot.Slot;
import org.nullauthor.allthings.block.entity.EffectUpgradeEntity;
import org.nullauthor.allthings.mods.ModItems;
import org.nullauthor.allthings.mods.ModScreenHandlerTypes;

public class EffectUpgradeScreenHandler extends ScreenHandler {
    protected final PlayerEntity player;
    protected final Inventory blockInventory;

    public EffectUpgradeScreenHandler(int syncId, PlayerInventory playerInventory) {
        this(syncId, playerInventory, new SimpleInventory(EffectUpgradeEntity.getInventorySize()));
    }

    public EffectUpgradeScreenHandler(int syncId, PlayerInventory playerInventory, Inventory blockInventory) {
        super(ModScreenHandlerTypes.EFFECT_UPGRADE_SCREEN, syncId);
        this.player = playerInventory.player;
        this.blockInventory = blockInventory;

        this.addInputSlots();
        this.addResultSlot();
        this.addPlayerInventorySlots(playerInventory);
    }

    private void addInputSlots() {
        //输入槽0：放置升级物品（幻梦残片等）
        this.addSlot(new Slot(this.blockInventory, 0, 20, 17){
            @Override
            public boolean canInsert(ItemStack stack) {
                return stack.getTranslationKey().equals(ModItems.DREAM_FRAGMENT.getTranslationKey());
            }
        });
        //输入槽0：放置媒介物品（木头、石头、铁、黄金、钻石等）
        this.addSlot(new Slot(this.blockInventory, 1, 56, 17) {
            @Override
            public boolean canInsert (ItemStack stack){
                String translationKey = stack.getTranslationKey();
                return (translationKey.toLowerCase().contains("minecraft") && translationKey.toLowerCase().contains("wood")) ||
                        translationKey.equalsIgnoreCase("minecraft:iron_block") ||
                        translationKey.equalsIgnoreCase("minecraft:gold_block") ||
                        translationKey.equalsIgnoreCase("minecraft:diamond_block") ||
                        translationKey.equalsIgnoreCase("minecraft:emerald_block") ||
                        translationKey.equalsIgnoreCase("minecraft:netherite_ingot") ||
                        translationKey.equalsIgnoreCase("minecraft:netherite_block");
            }
        });
        //输入槽0：放置催化物品（秘影粉尘等）
        this.addSlot(new Slot(this.blockInventory, 2, 38, 52){
            @Override
            public boolean canInsert(ItemStack stack) {
                return stack.getTranslationKey().equals(ModItems.SECRET_SHADOW_DUST.getTranslationKey());
            }
        });
    }

    private void addResultSlot() {
        this.addSlot(
                new Slot(this.blockInventory, 3, 116, 35) {
                    @Override
                    public boolean canInsert(ItemStack stack) {
                        return stack.getItem() instanceof ToolItem || stack.getItem() instanceof ArmorItem;
                    }

                    @Override
                    public boolean canTakeItems(PlayerEntity playerEntity) {
                        return true;
                    }

                    @Override
                    public void onTakeItem(PlayerEntity player, ItemStack stack) {
                        EffectUpgradeScreenHandler.this.onTakeOutput(player, stack);
                        super.onTakeItem(player, stack);
                    }
                }
        );
    }

    private void addPlayerInventorySlots(PlayerInventory playerInventory) {
        for (int i = 0; i < 3; i++) {
            for (int j = 0; j < 9; j++) {
                this.addSlot(new Slot(playerInventory, j + i * 9 + 9, 8 + j * 18, 84 + i * 18));
            }
        }

        for (int i = 0; i < 9; i++) {
            this.addSlot(new Slot(playerInventory, i, 8 + i * 18, 142));
        }
    }

    private void onTakeOutput(PlayerEntity player, ItemStack stack) {
        stack.onCraft(this.player.getWorld(), this.player, 1);
    }

    @Override
    public ItemStack quickMove(PlayerEntity player, int slot) {
        ItemStack newStack = ItemStack.EMPTY;
        Slot currentSolt = this.slots.get(slot);
        if (currentSolt.hasStack()) {
            ItemStack originalStack = currentSolt.getStack();
            newStack = originalStack.copy();
            if (slot < this.blockInventory.size()) {
                if (!this.insertItem(originalStack, this.blockInventory.size(), this.slots.size(), true)) {
                    return ItemStack.EMPTY;
                }
            } else if (!this.insertItem(originalStack, 0, this.blockInventory.size(), false)) {
                return ItemStack.EMPTY;
            }

            if (originalStack.isEmpty()) {
                currentSolt.setStack(ItemStack.EMPTY);
            } else {
                currentSolt.markDirty();
            }
        }

        return newStack;
    }

    @Override
    public boolean canUse(PlayerEntity player) {
        return this.blockInventory.canPlayerUse(player);
    }
}
